<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">物料号</label>
        <el-input v-model="query.materialCode" clearable placeholder="物料号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">容器编码</label>
        <el-input v-model="query.containerCode" clearable placeholder="容器编码" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">领料单号</label>
        <el-input v-model="query.pickingNo" clearable placeholder="领料单号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">工单号</label>
        <el-input v-model="query.workOrderNo" clearable placeholder="工单号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">生产线码</label>
        <el-input v-model="query.productLineCode" clearable placeholder="生产线码" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">行号</label>
        <el-input v-model="query.lineNo" clearable placeholder="行号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-upload
          ref="uploadRef"
          slot="right"
          class="filter-item"
          :headers="headers"
          :action="uploadUrl"
          :limit="1"
          :show-file-list="false"
          :http-request="customUpload"
        >
          <el-button
            type="success"
            size="mini"
            :loading="btn.upload"
            icon="el-icon-upload2"
            plain
            @click="handleUpload"
          >上传物料表</el-button>
        </el-upload>
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="id">
            <el-input v-model="form.id" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="物料号">
            <el-input v-model="form.materialCode" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="物料数量">
            <el-input v-model="form.materialNum" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="容器编码">
            <el-input v-model="form.containerCode" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="领料单号">
            <el-input v-model="form.pickingNo" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="工单号">
            <el-input v-model="form.workOrderNo" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="生产线码">
            <el-input v-model="form.productLineCode" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="行号">
            <el-input v-model="form.lineNo" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="入库时间">
            <el-input v-model="form.inboundDate" style="width: 250px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <!-- <el-table-column prop="id" label="id" /> -->
        <el-table-column prop="materialCode" label="物料号" />
        <el-table-column prop="materialNum" label="物料数量" />
        <el-table-column prop="containerCode" label="容器编码" />
        <el-table-column prop="pickingNo" label="领料单号" />
        <el-table-column prop="workOrderNo" label="工单号" />
        <el-table-column prop="productLineCode" label="生产线码" />
        <el-table-column prop="lineNo" label="行号" />
        <el-table-column prop="inboundDate" label="入库时间" />
        <!-- <el-table-column v-if="checkPer(['admin','stockMaterial:edit','stockMaterial:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column> -->
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import crudStockMaterial from '@/api/work/stockMaterial'
import { importExcelAPI } from '@/api/work/stockMaterial'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
// import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, materialCode: null, materialNum: null, containerCode: null, pickingNo: null, workOrderNo: null, productLineCode: null, lineNo: null, inboundDate: null }
export default {
  name: 'StockMaterial',
  components: { pagination, crudOperation, rrOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'stock_material', url: 'api/stockMaterial', idField: 'id', sort: 'id,desc', crudMethod: { ...crudStockMaterial }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'stockMaterial:add'],
        edit: ['admin', 'stockMaterial:edit'],
        del: ['admin', 'stockMaterial:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'materialCode', display_name: '物料号' },
        { key: 'containerCode', display_name: '容器编码' },
        { key: 'pickingNo', display_name: '领料单号' },
        { key: 'workOrderNo', display_name: '工单号' },
        { key: 'productLineCode', display_name: '生产线码' },
        { key: 'lineNo', display_name: '行号' }
      ],
      headers: {
        'Authorization': getToken(),
        'Content-Type': 'multipart/form-data'
      },
      uploadUrl: process.env.VUE_APP_BASE_API + '/api/import/storage',
      btn: {
        upload: false
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    async customUpload(file) {
      const formData = new FormData()
      formData.append('file', file.file)
      const res = await importExcelAPI({
        formData: formData
      })
      if (res.returnCode === '1') {
        this.$message.success(res.returnReason)
      } else {
        this.$message.warning(res.returnReason)
      }
    },
    // 清除已选择的文件
    handleUpload() {
      this.$refs.uploadRef.clearFiles() // 清除已选择的文件
      this.$refs.uploadRef.submit() // 重新提交上传
    }
  }
}
</script>

<style scoped>

</style>
